﻿///////////////////////////////////////////////////////////////
//                                                           //
// Code from Arcanea Project.                                //
//                                                           //
// Please include this license part to every reproduction    //
//   of code.                                                //
//                                                           //
// This code is under Ms-RL license                          //
//                                                           //
// Please read futher information about the license,         //
//   the project (news and updates) and the                  //
//   contributors on the address :                           //
//     http://www.codeplex.com/Arcanea/                      //
//                                                           //
///////////////////////////////////////////////////////////////
using System;
using System.Collections.Generic;
//using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Arcanea.Mediator.XmlClasses
{
    /// <summary>
    /// Classe qui definit un champ
    /// </summary>
    [XmlRoot]
    public class XmlField
    {

        private string name;
        /// <summary>
        /// Nom du champ
        /// </summary>
        [XmlElement]
        public string Name { get { return name; } set { name = value; } }

        #region System type

        private string systemType;
        /// <summary>
        /// Type du field (Nested ou de base)
        /// </summary>
        public string SystemType { get { return systemType; } set { systemType = value; } }

        #endregion

        private string description;
        /// <summary>
        /// Description du champ
        /// </summary>
        [XmlElement]
        public string Description { get { return description; } set { description = value; } }

        private bool notNull;
        /// <summary>
        /// True si le champ ne dois pas etre null
        /// </summary>
        [XmlElement]
        public bool NotNull { get { return notNull; } set { notNull = value; } }

        private bool isIDPK;
        /// <summary>
        /// True si c est un IDPK de son paquet
        /// </summary>
        [XmlElement]
        public bool IsIDPK { get { return isIDPK; } set { isIDPK = value; } }



        public override string ToString()
        {
            return FixeLength(Name, 30, false) + " ----- " + FixeLength(SystemType, 20, true) + " ----- " +
                (IsIDPK ? "IDPK" : "") + " - " + (NotNull ? "Not null" : ""); ;
        }

        #region Private functions

        private string FixeLength(object o, int length, bool centred)
        {
            string s = o.ToString();

            if (s.Length < length)
            {
                int todo = length - s.Length;
                if (centred)
                {
                    if (todo % 2 == 0)
                    {
                        return new string(' ', todo / 2) + s + new string(' ', todo / 2);
                    }
                    else
                    {
                        return new string(' ', todo/2) + s + new string(' ', todo/2 + 1);
                    }
                }
                else
                    return s + new string(' ', todo);
            }
            else return s.Substring(0, length); ;
        }

        #endregion
    }
}
